﻿using ProjectCloth.Entity;
using ProjectCloth.Entity.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class SaleToInstockFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public SaleToInstockFrm()
        {
            InitializeComponent();
            DataGridViewComboBoxColumn GenderColumn = Unit as DataGridViewComboBoxColumn;
            GenderColumn.Items.Add("米");
            GenderColumn.Items.Add("码");
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            var startTime = startDatePiker.Value.Date;
            var txtproductno = txtProductNo.Text;
            var endTime = endDatePicker.Value.AddDays(1).Date;
          var sql = (from s in db.SaleOrderItems.Where(o => (o.OrderItemStatus == (int)ItemStatus.TryInstock || (SelCheckbox.Checked == true && o.OrderItemStatus == (int)ItemStatus.Suspend))).ToList()
                                        join p in db.Products.ToList() on s.ProductId equals p.Id
                                        join o in db.SaleOrders.Where(t => t.OrderStatus != (int)OrderStatus.Delete && (t.RecordDate > startTime && t.RecordDate < endTime)&&(string.IsNullOrEmpty(orderNoTxt.Text)||t.OrderNo.Contains(orderNoTxt.Text.ToUpper()))).ToList() on s.OrderNo equals o.OrderNo
                                        select new
                                        {
                                            s.OrderNo,
                                            s.Quantity,
                                            s.ColorType,
                                            o.InMoneyDate,
                                            o.Remark,
                                            ItemRemark = s.Remark,
                                            Unit = EnumDefine.GetEnumDescription((Unit)s.Unit),
                                            p.ProductNo,
                                            OrderItemId = s.Id
                                        }).ToList();
            dataGridView1.DataSource = sql.Where(o => (string.IsNullOrEmpty(txtproductno) || o.ProductNo.Contains(txtproductno.ToUpper()))).ToList();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                #region  开始进货、修改进货
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn)
                {
                    var itemId = dataGridView1.CurrentRow.Cells["OrderItemId"].Value;
                    if (itemId != null && (int)itemId > 0)
                    {
                        var OrderNo = dataGridView1.CurrentRow.Cells["OrderNo"].Value as string;
                        var saleOrder = db.SaleOrders.Where(o => o.OrderNo == OrderNo).FirstOrDefault();
                        if(saleOrder==null|| saleOrder.OrderStatus != (int)OrderStatus.Checked)
                        {
                            MessageBox.Show("请确认订单是否存在或则订单状态是否为已审核！");
                            return;
                        }                       
                        if (column.Name == "ToInstock")
                        {
                            var main = MainFrm.CreateInstrance();
                            var tab = main.Controls.Find("tabControl1", true).FirstOrDefault() as TabControl;
                            var tabControl = tab;
                            if (tab != null)
                            {
                                tabControl = tab as TabControl;
                            }
                            dataGridView1.CurrentRow.Selected = true;
                            var instockfrm = new InStockItemFrm((int)itemId);
                            instockfrm.StartPosition = FormStartPosition.Manual;
                            instockfrm.Location = new Point(this.Right + 400 - instockfrm.Width, this.Bottom - instockfrm.Height);
                            instockfrm.ShowDialog(this);//或f.Show(this);或f.Show()
                            searchBtn_Click(null, null);
                        }
                        else if (column.Name == "Suspend")
                        {
                            var itemorder = db.SaleOrderItems.Where(o => o.Id == (int)itemId && o.OrderItemStatus == (int)ItemStatus.TryInstock).FirstOrDefault();
                            if (itemorder != null)
                            {
                                itemorder.OrderItemStatus = (int)ItemStatus.Suspend;
                                db.SaleOrderItems.Attach(itemorder);
                                db.Entry(itemorder).State = System.Data.Entity.EntityState.Modified;
                                if (db.SaveChanges() > 0)
                                {
                                    MessageBox.Show("屏蔽成功！");
                                };
                            }
                            else
                            {
                                MessageBox.Show("屏蔽失败！");

                            }
                            searchBtn_Click(null, null);
                        }
                        else if (column.Name == "OrderNo")
                        {
                            var orderNo = dataGridView1.CurrentRow.Cells["OrderNo"].Value;
                            if (orderNo != null)
                            {
                                var show = new SaleOrderShow(orderNo.ToString());
                                show.Show();
                            }
                        }


                    }

                }
                else //if(column is DataGridViewColumn)
                {
                    if (column.Name == "OrderNo")
                    {
                        var orderNo = dataGridView1.CurrentRow.Cells["OrderNo"].Value;
                        if (orderNo != null)
                        {
                            var show = new SaleOrderShow(orderNo.ToString());
                            show.Show();
                        }
                    }

                }
                #endregion

            }
        }

        private void dataGridView1_Sorted(object sender, EventArgs e)
        {
            //this.dataGridView1.Sort(this.dataGridView1.Columns[1], ListSortDirection.Ascending);
        }

        private void dataGridView1_AllowUserToOrderColumnsChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var slist = (from s in db.SaleOrderItems.Where(o => o.OrderItemStatus == (int)ItemStatus.TryInstock && (o.CreateDate > startDatePiker.Value && o.CreateDate < endDatePicker.Value)).ToList()
                         join p in db.Products.ToList() on s.ProductId equals p.Id
                         join o in db.SaleOrders.Where(t => t.OrderStatus != (int)OrderStatus.Delete).ToList() on s.OrderNo equals o.OrderNo
                         select new
                         {
                             s.OrderNo,
                             s.Quantity,
                             s.ColorType,
                             o.Remark,
                             ItemRemark = s.Remark,
                             Unit = EnumDefine.GetEnumDescription((Unit)s.Unit),
                             p.ProductNo,
                             OrderItemId = s.Id
                         }).ToList();
            dataGridView1.AutoGenerateColumns = false;
            if (e.ColumnIndex == 0)
            {
                dataGridView1.DataSource = slist.OrderBy(o => o.OrderNo).ToList();
            }
            else if (e.ColumnIndex == 1)
            {
                dataGridView1.DataSource = slist.OrderBy(o => o.Remark).ToList();
            }
            else if (e.ColumnIndex == 2)
            {
                dataGridView1.DataSource = slist.OrderBy(o => o.ProductNo).ToList();
            }

        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
}
